import pytest
from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
from common.log_tools import log
from common.loginpage import LoginPage
from common.mainpage import MainPage
from common.yaml_tools import YamlTools
from common.BackOfficeManagement import BackOfficeMana
from common.adduser import Adduser
from common.addBlock import Addblock

# login_data = [
#     {'用例编号':"login001","用例标题":"输入正确的用户名和密码,登录成功","username":"admin","password":"123456","expect":"7"},
#     {'用例编号':"login002","用例标题":"输入空的用户名和密码,登录失败","username":"","password":"","expect":"登录失败，请检查您的成员名或密码是否填写正确。"},
#     {'用例编号':"login003","用例标题":"输入正确的用户名和错误密码,登录成功","username":"admin","password":"666666","expect":"登录失败，请检查您的成员名或密码是否填写正确。"},
#     {'用例编号':"login004","用例标题":"输入正确的用户名和空密码,登录成功","username":"admin","password":"","expect":"登录失败，请检查您的成员名或密码是否填写正确。"},
# ]
# - expect: s-menu-superadmin
#   password: '123456'
#   title: 1.输入正确的用户名和密码，登录成功。
#   username: admin
# - expect: 登录失败，请检查您的成员名或密码是否填写正确。
#   password: '1234'
#   title: 2. 输入错误的的用户名和正确的密码,提示输出错误。
#   username: admin
# - expect: 登录失败，请检查您的成员名或密码是否填写正确。
#   password: ''
#   title: 3.输入空的用户名和空的密码，提示输出错误。
#   username: ''

login_data = YamlTools.read_yaml_file("login_data.yaml")

print(login_data)
adduser_Data = YamlTools.read_yaml_file("adduserData.yaml")
@pytest.mark.parametrize("abc",login_data)
def test_login_001(get_driver,abc):
    # log.info(f"执行登录接口的测试用例{abc.get('用例标题')}")
    print(login_data)
    log.info(login_data)
    driver = get_driver
    driver.find_element(By.ID,"account").send_keys(abc["username"])
    driver.find_element(By.ID,"password").send_keys(abc["password"])
    driver.find_element(By.ID,"submit").click()
    sleep(3)
    log.info(abc["expect"])
    if abc["expect"]=="s-menu-superadmin":
        assert_text = driver.find_element(By.ID,"s-menu-superadmin").get_attribute("data-order")
        assert assert_text == '7',"异常文本，查询失败"
        log.info(f"assert_text={assert_text}")

    else:
        sleep(3)

        assert_text = driver.find_element(By.XPATH,'/html/body/div[2]/div/div/div[1]/div').text
        assert assert_text == abc["expect"],"异常文本。查询失败"
@pytest.mark.parametrize("abc",login_data)
def test_login_002(get_driver,abc):
    log.info(f"执行登录接口的测试用例:{abc.get('用例标题')}")
    driver = get_driver
    loginpage = LoginPage(driver)
    mainpage = MainPage(driver)
    loginpage.enter_account(abc["username"])
    sleep(2)
    loginpage.enter_password(abc["password"])
    sleep(2)
    loginpage.click_login_button()
    sleep(5)
    log.info(f"abcexpect{abc["expect"]}")
    if abc["expect"] == "s-menu-superadmin":
        assert_text = mainpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == '7', "异常文本，查询失败"
        sleep(5)
    else:
        sleep(3)
        assert_text = loginpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == abc["expect"], "异常文本。查询失败"
@pytest.mark.parametrize("abc",login_data)
def test_enter_back001(get_driver,abc):
    log.info(f"执行登录接口的测试用例:{abc.get('用例标题')}")
    driver = get_driver
    loginpage = LoginPage(driver)
    mainpage = MainPage(driver)
    backofficeMana = BackOfficeMana(driver)
    loginpage.enter_account(abc["username"])
    sleep(2)
    loginpage.enter_password(abc["password"])
    sleep(2)
    loginpage.click_login_button()
    sleep(5)
    log.info(f"abcexpect{abc["expect"]}")
    if abc["expect"] == "s-menu-superadmin":
        assert_text = mainpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == '7', "异常文本，查询失败"
        sleep(5)
        mainpage.click_superadmin()
        sleep(5)
        backofficeMana.switch_toDefault_content()
        backofficeMana.enterBackIframe()
        backofficeMana.click_userloc()
        sleep(5)
    else:
        sleep(3)
        assert_text = loginpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == abc["expect"], "异常文本。查询失败"
@pytest.mark.parametrize("abc",login_data)
def test_enter_adduser002(get_driver,abc):
    log.info(f"执行登录接口的测试用例:{abc.get('用例标题')}")
    driver = get_driver
    loginpage = LoginPage(driver)
    mainpage = MainPage(driver)
    backofficeMana = BackOfficeMana(driver)
    adduser = Adduser(driver)
    loginpage.enter_account(abc["username"])
    sleep(2)
    loginpage.enter_password(abc["password"])
    sleep(2)
    loginpage.click_login_button()
    sleep(5)
    log.info(f"abcexpect{abc["expect"]}")
    if abc["expect"] == "s-menu-superadmin":
        assert_text = mainpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == '7', "异常文本，查询失败"
        sleep(5)
        mainpage.click_superadmin()
        sleep(5)
        backofficeMana.switch_toDefault_content()
        backofficeMana.enterBackIframe()
        backofficeMana.click_userloc()
        sleep(5)
    else:
        sleep(3)
        assert_text = loginpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == abc["expect"], "异常文本。查询失败"
    adduser.switch_toDefault_content()
    adduser.enterBackIframe()
    adduser.enter_username('name111')
    adduser.enter_realname('name123')
    adduser.click_sex()
    adduser.select_role()
    adduser.enter_password()
    adduser.enter_repassword()
    adduser.enter_email('155@qq.com')
    adduser.click_submit()
    sleep(5)
    try:
        assert_text_addSuccess = adduser.get_assertSuccessText()
        log.info('新增成功')
    except Exception:
        log.info('新增失败了哦')
@pytest.mark.parametrize("abc",login_data)
def test_addBlock(get_driver,abc):
    log.info(f"执行登录接口的测试用例:{abc.get('用例标题')}")
    driver = get_driver
    loginpage = LoginPage(driver)
    mainpage = MainPage(driver)
    addBlock = Addblock(driver)
    loginpage.enter_account(abc["username"])
    sleep(2)
    loginpage.enter_password(abc["password"])
    sleep(2)
    loginpage.click_login_button()
    sleep(5)
    log.info(f"abcexpect{abc["expect"]}")
    if abc["expect"] == "s-menu-superadmin":
        assert_text = mainpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == '7', "异常文本，查询失败"
        sleep(5)
    else:
        sleep(3)
        assert_text = loginpage.get_assert_text()
        log.info(f"assert_text={assert_text}")
        assert assert_text == abc["expect"], "异常文本。查询失败"

    sleep(5)
    addBlock.click_AddUser()
    sleep(3)
    addBlock.select_Allentries()
    sleep(3)
    addBlock.select_entryBlockLoc()
    sleep(3)
    addBlock.click_submit()
def test_login_001():
    # from selenium.webdriver.common.by import By
    # from selenium import webdriver
    # from time import sleep
    # from selenium.webdriver.support.ui import Select
    driver = webdriver.Chrome()
    driver.get('http://192.168.1.37/ranzhi/www/sys/user-login.html')
    username = driver.find_element(By.ID, 'account')
    username.send_keys('admin')
    password = driver.find_element(By.ID, 'password')
    password.send_keys('123456')
    login = driver.find_element(By.ID, 'submit')
    login.click()
    # driver.get('http://192.168.1.37/ranzhi/www/sys/user-create.html')
    # sleep(5)
    # superadmin = driver.find_element(By.ID, 's-menu-superadmin')
    # superadmin.click()
    # sleep(5)
    # driver.switch_to.frame('iframe-superadmin')
    # addUser = driver.find_element(By.XPATH, '//*[@id="shortcutBox"]/div/div[1]/div/a')
    # addUser.click()
    # sleep(5)
    # # def send_id_keys(id,keys):
    # #     selected = driver.find_element(By.ID, id)
    # #     selected.send_keys(keys)
    # ##########添加成员页面
    # account = driver.find_element(By.ID, 'account')
    # account.send_keys('hhhhh')
    # realname = driver.find_element(By.ID, 'realname')
    # realname.send_keys('hhhh12')
    # sexM = driver.find_element(By.ID, 'genderm')
    # genderf = driver.find_element(By.ID, 'genderf')
    # sexM.click()
    # # 1.
    # driver.find_element(By.ID,"role").send_keys("研发")
    # # #2.
    # # driver.find_element(By.ID,"role").click()
    # # sleep(2)
    # # driver.find_element(By.XPATH,'//*[@id="role"]/option[2]').click()
    # # #3.
    #
    # # 4.
    # # roleSelect = Select(driver.find_element(By.ID,'role'))
    # # roleSelect.select_by_value('dev')
    # password1 = driver.find_element(By.ID, 'password1')
    # password1.send_keys('123456')
    # sleep(2)
    # password2 = driver.find_element(By.ID, 'password2')
    # password2.send_keys('123456')
    # sleep(2)
    # email = driver.find_element(By.ID, 'email')
    # email.send_keys('123677886786@qq.com')
    # submit = driver.find_element(By.ID, 'submit')
    # submit.click()
    # sleep(5)
    # driver.switch_to.default_content()
    # driver.switch_to.frame("iframe-superadmin")
    # sleep(5)
    # assert_text = driver.find_element(By.XPATH,'/html/body/div/div/div/div[2]/div/div/div[1]/div/a').text
    # assert assert_text=='assert_text','没有保存成功'

    sleep(5)
    driver.quit()






